package com.neuedu.jdbc.v3.dao;

import com.neuedu.jdbc.v3.DBUtils;
import com.neuedu.jdbc.v3.entity.Menu;

import java.util.List;
import java.util.Map;

public class MenuDao {

    /**
     * 根据条件查询集合
     * @param where 条件
     * @param params 绑定变量的参数
     * @return 集合
     */
    //where  =      where sss = ?
    public List<Menu> selectList(String where ,Object ... params){
        String sql =" select *  from menu ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        System.out.println("sql = " + sql);
        List<Menu> menus = DBUtils.selectList(Menu.class, sql, params);

        return menus;
    }

    /**
     * 根据条件查询一个
     * @param where
     * @param params
     * @return
     */
    public Menu selectOne(String where ,Object ... params){
        String sql =" select * from menu ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(Menu.class, sql, params);
    }


    /**
     * 查询总条数
     * @param where
     * @param params
     * @return
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from menu ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }

    /**
     * 根据数据进行插入
     * @param menu
     * @return
     */
    public int insert(Menu menu){
        String sql = "INSERT INTO   `menu` (  `menu_id`, `menu_name`,  `url`,  `parent_id`, `active`, `createtime` )  VALUES ( ?,?,?,?,?,? ) " ;
        return DBUtils.update( sql,
                menu.getMenuId(),
                menu.getMenuName(),
                menu.getUrl(),
                menu.getParentId(),
                menu.getActive(),
                menu.getCreatetime()
                );
    }


    /**
     *  批量插入
     * @param menuList
     * @return
     */
    public int insertBatch(List<Menu> menuList){
        String sql = "INSERT INTO   `menu` (  `menu_id`, `menu_name`,  `url`,  `parent_id`, `active`, `createtime` )  VALUES ( ?,?,?,?,?,? )";
        int count = 0;
        for (Menu menu : menuList) {
            count += DBUtils.update( sql,
                    menu.getMenuId(),
                    menu.getMenuName(),
                    menu.getUrl(),
                    menu.getParentId(),
                    menu.getActive(),
                    menu.getCreatetime()
            );

        }
        return count;
    }


    /**
     * 根据主键更新一条
     * @param menu
     * @return
     */
    public int updateById(Menu menu){
        String sql = " UPDATE  `menu`  SET `menu_name` = ?,  `url` = ?, `parent_id` = ?, `active` = ?, `createtime` = ?, WHERE `menu_id` = ?  ";
        return DBUtils.update( sql,
                menu.getMenuName(),
                menu.getUrl(),
                menu.getParentId(),
                menu.getActive(),
                menu.getCreatetime(),
                menu.getMenuId());

    }


    /**
     * 根据主键更新一条
     * @param id
     * @return
     */
    public int deleteById(Integer id){
        String sql = " delete from menu  where menu_id = ?";
        return DBUtils.update( sql, id);

    }


    /**
     * 根据条件删除
     * @param where
     * @param params
     * @return
     */
    public int delete(String where ,Object... params){
        String sql = " delete from menu ";
        if(where == null || "".equals(where)){
            throw new RuntimeException("必须有条件删除");
        }
        sql += where;
        return DBUtils.update( sql, params);

    }

}
